package com.dmall.vue.service.impl;

import com.dmall.vue.dao.UserRepository;
import com.dmall.vue.domain.User;
import com.dmall.vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author Mr.Wang
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User findOne(Integer id) {
        Optional<User> user = userRepository.findById(id);
        return user.get();
        /*Specification<User> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<Object> idPath = root.get("id");
            Predicate predicate = criteriaBuilder.equal(idPath, id);
            return predicate;
        };
        return userRepository.findOne(spec).get();*/
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(User user) {
        userRepository.save(user);
    }
}
